package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/05/02/上午 11:58;
 * Description
 */
@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车食品
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "user_id", BaseContext.getCurrentId());

        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());

        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id", shoppingCart.getDishId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart == null) {
            //为空 添加新的
            shoppingCart.setUserId(BaseContext.getCurrentId());

            shoppingCart.setNumber(1);

            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);
        } else {
            //存在时
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成果");
    }
//删除逻辑
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart--------{}",shoppingCart);
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "user_id", BaseContext.getCurrentId());

        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());

        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id", shoppingCart.getDishId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        cart.setNumber(cart.getNumber() - 1);
        if (cart.getNumber()==0){
            shoppingCartService.removeById(cart);
        }
        shoppingCartService.updateById(cart);
        return  R.success("减少成果");
    }
    /**
     * 查询购物车
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.orderByAsc("create_time");

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }

    /**清空购物车
     *
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        boolean remove = shoppingCartService.remove(queryWrapper);

        return R.success("清空成果");
    }

}
